package com.cjq.controller;

import com.cjq.bean.*;
import com.cjq.mvc.ResponseBody;
import com.cjq.service.CourierService;
import com.cjq.service.ExpressService;
import com.cjq.util.DateFormatUtil;
import com.cjq.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierController {

    //统计信息
    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest req, HttpServletResponse resp){
        List<Map<String, Integer>> data = CourierService.console();

        Message msg = new Message();
        if (data.size()==0){
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;

    }


    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest req, HttpServletResponse resp){
        // 获取查询数据的起始索引值
        int offset = Integer.parseInt(req.getParameter("offset"));
        //每一页查询的数量
        int pageNumber = Integer.parseInt(req.getParameter("limit"));
        //进行查询
        List<Courier> list = CourierService.findAll(true, offset, pageNumber);
        //将 返回的结果的数据格式  转换为前台能更好显示的格式，都装换为String，所以我又创建一个bean类BootStrapExpress 来代替Express类，
        //将返回的结果以  List<BootStrapExpress>  的数据类型返回   相当于换了一个list
        List<BootStrapTableCourier> list2 = new ArrayList<>();
        for (Courier c : list) {

            String createTime = DateFormatUtil.format(c.getCreateTime());
            String loginTime = c.getLoginTime()==null?"未有登录记录":DateFormatUtil.format(c.getLoginTime());
            String jianshu = c.getJianShu()==null?"0": String.valueOf(c.getJianShu());


            BootStrapTableCourier c1 = new BootStrapTableCourier(c.getId(), c.getSysName(), c.getSysPhone(), c.getCardId(), c.getPassword(),jianshu, createTime, loginTime);
            list2.add(c1);
        }

        //获取总条数
        List<Map<String, Integer>> console = CourierService.console();    //这里获得的是一个map类型的集合，但实际只有一个map 这个map有两条数据 快递员总人数：n  今日注册人数：m
        Integer total = console.get(0).get("data1_size");
        //将集合为 bootstrap-table识别的格式
        ResultDate<BootStrapTableCourier> data = new ResultDate<>();

        data.setRows(list2);
        data.setTotal(total);
        String json = JSONUtil.toJSON(data);
        return json;
    }


    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest req, HttpServletResponse resp){
        String sysName = req.getParameter("sysName");
        String sysPhone = req.getParameter("sysPhone");
        String cardId = req.getParameter("cardId");
        String password = req.getParameter("password");


        Courier courier = new Courier(sysName, sysPhone, cardId, password);

        boolean flay = CourierService.insert(courier);
        Message msg = new Message();
        if (flay){
            //录入成功
            msg.setStatus(0);
            msg.setResult("快递员录入成功");
        }else{
            //录入失败
            msg.setStatus(-1);
            msg.setResult("快递员录入失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }


    @ResponseBody("/courier/findBySysPhone.do")
    public String find(HttpServletRequest req, HttpServletResponse resp){
        String sysPhone = req.getParameter("sysPhone");
        Courier c = CourierService.findBySysPhone(sysPhone);
        Message msg = new Message();
        System.out.println(c);
        if (c==null){
            msg.setStatus(-1);
            msg.setResult("查询的电话不存在");
        }else {
            msg.setStatus(0);
            msg.setResult("查询成功");
            msg.setData(c);
        }
        String json = JSONUtil.toJSON(msg);
        return json;

    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        String sysName = req.getParameter("sysName");
        String sysPhone = req.getParameter("sysPhone");
        String cardId = req.getParameter("cardId");
        String password = req.getParameter("password");

        String g_sysPhone = req.getParameter("g_sysPhone");

        Courier courier = new Courier(id,sysName,sysPhone,cardId,password);



        boolean flay = CourierService.update(courier);     //修改电话的话，如果快递表有该快递员所负责的快递，那么那个sysPhone也要改为更改后的电话
        CourierService.updateExpressSysPhone(sysPhone,g_sysPhone);



        Message msg = new Message();
        if (flay){
            msg.setStatus(0);
            msg.setResult("修改成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("修改失败");
        }

        return JSONUtil.toJSON(msg);

    }


    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest req, HttpServletResponse resp){
        int id = Integer.parseInt(req.getParameter("id"));
        boolean flay = CourierService.delete(id);

        Message msg = new Message();
        if (flay){
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        return JSONUtil.toJSON(msg);
    }




}
